package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.PickingSaleDto;
import com.pureut.storage.export.PickingSaleExport;
import com.pureut.storage.service.PickingSaleService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.PickingSaleVo;
import com.pureut.storage.vo.UpdateDocVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 销售发货控制层
 * @author: CM
 * @date: 2022/12/27  14:42
 *
 */
@RestController
@RequestMapping("/pickingSale")
public class PickingSaleController extends BaseController {

    @Resource
    PickingSaleService pickingSaleService;

    /**
     * 新增销售发货单
     */
    @RequiresPermissions("warehouse:outbound:shipsale:add")
    @Log(title = "新增销售发货", businessType = BusinessType.INSERT)
    @PostMapping("/addSale")
    public AjaxResult addSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.addSale(pickingSaleVo));
    }

    /**
     * 修改销售发货单
     */
    @RequiresPermissions("warehouse:outbound:shipsale:edit")
    @Log(title = "修改销售发货", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSale")
    public AjaxResult updateSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.updateSale(pickingSaleVo));
    }

    /**
     * 关结销售发货单据
     */
    @RequiresPermissions("warehouse:outbound:shipsale:close")
    @Log(title = "关结单据", businessType = BusinessType.UPDATE)
    @PostMapping("/closeSale")
    public AjaxResult closeSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.closeSale(pickingSaleVo));
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:outbound:shipsale:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteSale/{ids}")
    public AjaxResult deleteSale(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingSaleService.deleteSale(ids));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("warehouse:outbound:shipsale:submit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitSale/{ids}")
    public AjaxResult submitSale(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingSaleService.submitSale(ids));
    }

    /**
     * 查看单据
     */
    @GetMapping("/view/{id}")
    @RequiresPermissions("warehouse:outbound:shipsale:query")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingSaleService.view(id));
    }

    /**
     * 查询销售发货列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(PickingSaleVo pickingSaleVo) {
        startPage();
        //查询销售发货列表
        List<PickingSaleDto> list = pickingSaleService.getList(pickingSaleVo);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:outbound:shipsale:export")
    @PostMapping("/export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response,PickingSaleVo pickingSaleVo) throws IOException {
        List<PickingSaleExport> infoExport = pickingSaleService.getInfoExport(pickingSaleVo);
        ExcelUtil<PickingSaleExport> excelUtil = new ExcelUtil<PickingSaleExport>(PickingSaleExport.class);
        excelUtil.excelWriter(response, infoExport, "销售发货信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 销售发货出库
     */
    @RequiresPermissions("warehouse:outbound:shipsale:outbound")
    @Log(title = "销售发货出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public AjaxResult out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingSaleService.out(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    @Log(title = "拣货完成", businessType = BusinessType.UPDATE)
    public AjaxResult finishProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingSaleService.finishProduction(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealProduction/{id}")
    @Log(title = "拣货撤销", businessType = BusinessType.UPDATE)
    public AjaxResult repealProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingSaleService.repealProduction(id));
    }

    /**
     * 出库交接
     */
    @RequiresPermissions("warehouse:outbound:shipsale:handover")
    @Log(title = "出库交接", businessType = BusinessType.UPDATE)
    @PostMapping("/overProduction")
    public AjaxResult overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingSaleService.overProduction(pickingRestDbVo));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("warehouse:outbound:shipsale:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/saleReview")
    public AjaxResult saleReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(pickingSaleService.saleReview(orderReviewVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/saleUpdateStatus")
    public AjaxResult saleUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingSaleService.saleUpdateStatus(updateDocVo));
    }

    /**
     * 拣货提示
     */
    @GetMapping("/point/{id}")
    public AjaxResult point(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingSaleService.point(id));
    }

    /**
     * 查询销售发货列表(feign调用) 客诉管理查询使用
     */
    @GetMapping("/ByCustomerComplaintsList")
    public AjaxResult getByCustomerComplaintsList(PickingSaleVo pickingSaleVo) {
        //查询销售发货列表
        return AjaxResult.success(pickingSaleService.getByCustomerComplaintsList(pickingSaleVo));
    }

}
